package com.spic.business.accident.analysisReport.service.impl;

import com.spic.business.accident.analysisReport.domain.AccidentAnalysisReport;
import com.spic.business.accident.analysisReport.domain.AccidentAnalysisReportDto;
import com.spic.business.accident.analysisReport.domain.AccidentAnalysisReportTreatmentSubtable;
import com.spic.business.accident.analysisReport.mapper.AccidentAnalysisReportMapper;
import com.spic.business.accident.analysisReport.service.IAccidentAnalysisReportService;
import com.spic.business.common.core.workflow.IWorkFlowType;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.common.workflow.WorkFlowServiceImpl;
import com.spic.business.enumconst.BusinessType;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.security.utils.cache.DeptCacheUtils;
import com.spic.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 事故分析报告Service业务层处理
 *
 * @author spic
 * @date 2021-11-30
 */
@Service
public class AccidentAnalysisReportServiceImpl extends WorkFlowServiceImpl<AccidentAnalysisReportMapper, AccidentAnalysisReport> implements IAccidentAnalysisReportService {
    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private AccidentAnalysisReportMapper accidentAnalysisReportMapper;

    /**
     * 查询事故分析报告
     *
     * @param id 事故分析报告主键
     * @return 事故分析报告
     */
    @Override
    public AccidentAnalysisReport selectAccidentAnalysisReportById(Long id) {
        AccidentAnalysisReport accidentAnalysisReport = accidentAnalysisReportMapper.selectAccidentAnalysisReportById(id);
        String accidentDepartment = accidentAnalysisReport.getAccidentDepartment();
        String accidentDepartmentName = null;
        try {
            accidentDepartmentName = DeptCacheUtils.getValue(Long.parseLong(accidentDepartment));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        accidentAnalysisReport.setAccidentDepartmentName(accidentDepartmentName);
        return accidentAnalysisReport;
    }

    /**
     * 查询事故分析报告列表
     *
     * @param accidentAnalysisReport 事故分析报告
     * @return 事故分析报告
     */
    @Override
    @BusinessDataScope(menuPath = "business/accident/analysisReport/index")
    public List<AccidentAnalysisReport> selectAccidentAnalysisReportList(AccidentAnalysisReport accidentAnalysisReport) {
        List<AccidentAnalysisReport> accidentAnalysisReportList = accidentAnalysisReportMapper.selectAccidentAnalysisReportList(accidentAnalysisReport);
        super.processRow(accidentAnalysisReportList, null);
        return accidentAnalysisReportList;
    }

    /**
     * 查询事故分析报告列表
     *
     * @param accidentAnalysisReport 事故分析报告
     * @return 事故分析报告
     */
    @Override
    public List<AccidentAnalysisReport> selectAccidentAnalysisReportListById(AccidentAnalysisReport accidentAnalysisReport) {
        List<AccidentAnalysisReport> accidentAnalysisReportList = accidentAnalysisReportMapper.selectAccidentAnalysisReportListById(accidentAnalysisReport);
        return accidentAnalysisReportList;
    }

    /**
     * 新增事故分析报告
     *
     * @param accidentAnalysisReport 事故分析报告
     * @return 结果
     */
    @Override
    public Long insertAccidentAnalysisReport(AccidentAnalysisReport accidentAnalysisReport) {
        accidentAnalysisReport.setCreateTime(DateUtils.getNowDate());
        accidentAnalysisReportMapper.insertAccidentAnalysisReport(accidentAnalysisReport);
        insertAccidentAnalysisReportTreatmentSubtable(accidentAnalysisReport);
        return accidentAnalysisReport.getId();
    }

    /**
     * 修改事故分析报告
     *
     * @param accidentAnalysisReport 事故分析报告
     * @return 结果
     */
    @Override
    public int updateAccidentAnalysisReport(AccidentAnalysisReport accidentAnalysisReport) {
        if (StringUtils.isNotNull(accidentAnalysisReport.getAccidentAnalysisReportTreatmentSubtableList()) && accidentAnalysisReport.getAccidentAnalysisReportTreatmentSubtableList().size() > 0) {
            accidentAnalysisReportMapper.deleteAccidentAnalysisReportTreatmentSubtableByParentId(accidentAnalysisReport.getId());
        }
        accidentAnalysisReport.setUpdateTime(DateUtils.getNowDate());
        accidentAnalysisReport.setUpdateBy(SecurityUtils.getUsername());
        insertAccidentAnalysisReportTreatmentSubtable(accidentAnalysisReport);
        return accidentAnalysisReportMapper.updateAccidentAnalysisReport(accidentAnalysisReport);
    }

    /**
     * 批量删除事故分析报告
     *
     * @param ids 需要删除的事故分析报告主键
     * @return 结果
     */
    @Override
    public int deleteAccidentAnalysisReportByIds(Long[] ids) {
        Arrays.stream(ids).forEach(id -> {
            AccidentAnalysisReport accidentAnalysisReport = selectAccidentAnalysisReportById(id);
            updateReportNum(accidentAnalysisReport, 1);
        });

        accidentAnalysisReportMapper.deleteAccidentAnalysisReportTreatmentSubtableIds(ids);

        return accidentAnalysisReportMapper.deleteAccidentAnalysisReportByIds(ids);
    }

    /**
     * 批量修改事故分析报告
     *
     * @param accidentAnalysisReportDto 需要修改的事故分析报告集合
     * @return 结果
     */
    @Override
    public int updateAccidentAnalysisReportByDto(AccidentAnalysisReportDto accidentAnalysisReportDto) {
        return accidentAnalysisReportMapper.updateAccidentAnalysisReportByDto(accidentAnalysisReportDto);
    }

    /**
     * 删除事故分析报告信息
     *
     * @param id 事故分析报告主键
     * @return 结果
     */
    @Override
    public int deleteAccidentAnalysisReportById(Long id) {
        accidentAnalysisReportMapper.deleteAccidentAnalysisReportTreatmentSubtableByParentId(id);
        return accidentAnalysisReportMapper.deleteAccidentAnalysisReportById(id);
    }

    /**
     * 获取本年份事故分析报告数量
     *
     * @param year 当前年分
     * @return 结果
     */
    @Override
    public List<AccidentAnalysisReport> countAnalysis(String year) {
        return accidentAnalysisReportMapper.countAnalysis(year);
    }


    /**
     * 隐藏事故分析报告数量
     *
     * @param accidentAnalysisReport
     * @return 结果
     */
    @Override
    public int coverReport(AccidentAnalysisReport accidentAnalysisReport) {
        updateReportNum(accidentAnalysisReport, Integer.parseInt(accidentAnalysisReport.getCoverFlag()));
        return accidentAnalysisReportMapper.updateAccidentAnalysisReport(accidentAnalysisReport);
    }

    /**
     * 删除隐藏时动态修改分析报告编号
     *
     * @param accidentAnalysisReport
     * @param coverFlag              隐藏或取消隐藏标识
     */
    @Override
    public void updateReportNum(AccidentAnalysisReport accidentAnalysisReport, int coverFlag) {
        int reportNumFlag = 0;
        if (coverFlag == 1) {
            reportNumFlag = -1;
        } else if (coverFlag == 0) {
            reportNumFlag = 1;
        }
        List<AccidentAnalysisReport> accidentAnalysisReportList = selectAccidentAnalysisReportListById(accidentAnalysisReport);
        int finalReportNumFlag = reportNumFlag;
        List<AccidentAnalysisReportDto> accidentAnalysisReportDtoList = accidentAnalysisReportList.stream()
                .map(item -> new AccidentAnalysisReportDto(item.getId(), item.getReportNum() + finalReportNumFlag))
                .collect(Collectors.toList());
        accidentAnalysisReportDtoList.forEach(accidentAnalysisReportDto -> {
            updateAccidentAnalysisReportByDto(accidentAnalysisReportDto);
        });
    }

    @Override
    protected IWorkFlowType getWorkFlowType() {
        return BusinessType.ANALYSIS_REPORT;
    }

    @Override
    protected void setWorkFlowVariable(AccidentAnalysisReport entity, Map<String, Object> variable) {
        variable.put("drafter", SecurityUtils.getUserId());
    }

    public void insertAccidentAnalysisReportTreatmentSubtable(AccidentAnalysisReport accidentAnalysisReport) {
        List<AccidentAnalysisReportTreatmentSubtable> accidentAnalysisReportTreatmentSubtableList = accidentAnalysisReport.getAccidentAnalysisReportTreatmentSubtableList();
        Long id = accidentAnalysisReport.getId();
        if (StringUtils.isNotNull(accidentAnalysisReportTreatmentSubtableList) && accidentAnalysisReportTreatmentSubtableList.size() > 0) {
            List<AccidentAnalysisReportTreatmentSubtable> list = new ArrayList<AccidentAnalysisReportTreatmentSubtable>();
            for (AccidentAnalysisReportTreatmentSubtable accidentAnalysisReportTreatmentSubtable : accidentAnalysisReportTreatmentSubtableList) {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                accidentAnalysisReportTreatmentSubtable.setId(uuid);
                accidentAnalysisReportTreatmentSubtable.setParentId(id);
                list.add(accidentAnalysisReportTreatmentSubtable);
            }
            if (list.size() > 0) {
                accidentAnalysisReportMapper.batchAccidentAnalysisReportTreatmentSubtable(list);
            }
        }
    }
}
